// pages/order/refund/refund.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderId: 0,
        content: '7天无理由退款',
        type: '10',
        refundGoodsIndexList: 0,
        refundTotalAmount: "0.00",
        detail: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.orderId = options.id;
        this.loadDetail();
    },

    // 内容被改变
    bindContentChange: function(e) {
        this.data.content = e.detail.value;
    },

    // 退款类型被改变
    bindTypeChange: function(e) {
        const type = e.detail.value;
        this.data.type = type;
        this.setData({
            type: type
        });
    },

    // 内容被改变
    bindGoodsChange: function(e) {
        const refundGoodsIndexList = e.detail.value;

        let refundTotalAmount = wx.BigNumber('0.00');
        refundGoodsIndexList.forEach(index => {
            const goods = this.data.detail.goods[index];
            refundTotalAmount = refundTotalAmount.plus(goods.total_pay_price)
        });
        refundTotalAmount = refundTotalAmount.toFixed(2, wx.BigNumber.ROUND_DOWN);

        this.data.refundGoodsIndexList = refundGoodsIndexList;
        this.setData({
            refundGoodsIndexList: e.detail.value,
            refundTotalAmount: refundTotalAmount
        });
    },

    // 加载订单详情
    loadDetail: function() {
        return wx.http.post('api/user.order/detail', {
            order_id: this.data.orderId
        }).then((res) => {
            const detail = res.data.data.order;
            if (detail.delivery_status.value === 20) {
                this.data.type = 10;
            }
            this.setData({
                detail: detail,
            });
            return detail;
        }, (err) => {
            console.warn("api/user.order/detail", err)
        });
    },

    // 保存并返回上一页
    saveReturn: function(e) {
        const refundGoodsIndexList = this.data.refundGoodsIndexList;
        if (!refundGoodsIndexList.length) {
            wx.showToast({
                title: '请选择退款/退货商品',
                icon: 'none'
            });
            return;
        }

        let content = this.data.content;
        if ('其它' === content) {
            content = e.detail.value.content;
        }
        if (content.trim() === '') {
            wx.showToast({
                title: '请输入退款原因',
                icon: 'none'
            });
            return;
        }

        const refundGoodsIdList = refundGoodsIndexList.map(index => {
            return this.data.detail.goods[index].order_goods_id
        });

        wx.http.post('api/user.refund/apply', {
            order_id: this.data.orderId,
            order_goods_id: refundGoodsIdList.join(','),
            content: content,
            type: this.data.type,
        }, {
            showLoading: true
        }).then((res) => {
            wx.showModal({
                content: '申请成功！',
                showCancel: false,
                success(res) {
                    wx.navigateBack({
                        delta: 1
                    })
                }
            })
        });
    },

});